#pragma once

#ifndef Uint8
typedef unsigned char Uint8;
#endif

#ifndef Uint8_Bitmask
typedef unsigned char Uint8_Bitmask;
#endif

#ifndef Byte
typedef unsigned char Byte;
#endif

#ifndef Uint16
typedef unsigned short Uint16;
#endif

#ifndef Sint16
typedef signed short Sint16;
#endif

#ifndef Uint32
typedef unsigned int Uint32;
#endif

#ifndef Sint32
typedef int Sint32;
#endif

#ifndef Uint64
typedef unsigned long long Uint64;
#endif

#ifndef Uint24_be
struct Uint24_be
{
	unsigned char _byte[3];
};
#endif

//#ifndef Uint24
//typedef Uint24_be Uint24;
//#endif

#ifndef Uint32_be
typedef unsigned int Uint32_be;
#endif

#ifndef VarUint32orUint64
struct VarUint32orUint64
{
	bool m_bIs64;
	union
	{
		Uint64 m_ui64;
		Uint32 m_ui32;
	};

	VarUint32orUint64()
	{
		memset(this, 0, sizeof(VarUint32orUint64));
	}

	void SetNumber(const Uint64 ui64Data, bool bIs64)
	{
		m_bIs64 = bIs64;
		if (m_bIs64)
		{
			m_ui64 = ui64Data;
		} 
		else
		{
			m_ui32 = (Uint32)ui64Data;
		}
	}

	Uint64 GetNumber()
	{
		if (m_bIs64)
		{
			return m_ui64;
		} 
		else
		{
			return (Uint64)m_ui32;
		}
	}
};
#endif

#ifndef FixedPoint32
typedef Uint32 FixedPoint32;
#endif

#ifndef FixedPoint16
typedef Uint16 FixedPoint16;
#endif


#define Uint16_be_to_Uint16(x) ( (Uint16)((x&0xFF)<<8) | ((x&0xFF00)>>8) )
#define Sint16_be_to_Sint16(x) ( (Sint16)((x&0xFF)<<8) | ((x&0xFF00)>>8) )


#define Uint24_be_to_Uint32(x) ((Uint32)(((x)._byte[0] << 16) | \
	((x)._byte[1] << 8) | (x)._byte[2]))

#define Uint32_to_Uint24_be(x, y)\
{\
	Uint32 ui32Tmp = x;\
	y._byte[0] = (unsigned char)(ui32Tmp & 0xFF);\
	y._byte[1] |= (unsigned char)((ui32Tmp & 0xFF00) >> 8);\
	y._byte[2] |= (unsigned char)((ui32Tmp & 0xFF0000) >> 16);\
}

#define Uint24_be_to_Uint64(x) ((Uint64)(((x)._byte[0] << 16) | \
	((x)._byte[1] << 8) | (x)._byte[2]) | (Uint64)0x0)

#define Uint32_be_to_Uint32(x) ((Uint32)(x&0xFF)<<24 |  (x&0xFF00)<<8 | (x&0xFF0000)>>8 | (x&0xFF000000)>>24 )

#define double_to_fixpoint32(x, y)\
{\
	Uint16 ui16FrontPointTmp = (Uint16)x;\
	double dbAfterPointTmp = x - (double)ui16FrontPointTmp;\
	dbAfterPointTmp *= 100000;\
\
	Uint16 ui16AfterPoint = (Uint16)dbAfterPointTmp;\
	y = (((Uint32)ui16FrontPointTmp) >> 16) | (((Uint32)ui16AfterPoint) & 0xFFFF);\
}
#define double_to_fixpoint16(x, y)\
{\
	Uint8 ui16FrontPointTmp = (Uint8)x;\
	double dbAfterPointTmp = x - (double)ui16FrontPointTmp;\
	dbAfterPointTmp *= 100000;\
\
	Uint8 ui16AfterPoint = (Uint8)dbAfterPointTmp;\
	y = (((Uint16)ui16FrontPointTmp) >> 8) | (((Uint16)ui16AfterPoint) & 0xFF);\
}

#define ClearMemBlock(x) memset(&x, 0, sizeof(x))


#define MAX_PTR_VALUE 0xFFFFFFFF
#define MAX_TYPE_VALUE 0xFF


template <class T>
T BoolToNumber(bool b)
{
	if (b)
	{
		return ((T)1);
	}

	return ((T)0);
}